import { Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { QuestionBase } from './question-base.model';
import { QuestionService } from './question.service';
import { QuestionControlService } from './question-control.service';

@Component({
    selector: 'dynamic-form',
    templateUrl: './dynamic-form.component.html',
    providers: [QuestionService, QuestionControlService]
})
export class DynamicFormComponent implements OnInit {
    @Input() questions: QuestionBase<any>[] = [];
    form: FormGroup;
    payLoad = '';

    constructor(private service: QuestionService, private qcs: QuestionControlService) {}

    ngOnInit(): void {
        this.questions = this.service.getQuestions();
        this.form = this.qcs.toFormGroup(this.questions);
    }

    onSubmit(): void {
        this.payLoad = JSON.stringify(this.form.value);
    }
}